package com.xiaoshuidi.cloud.module.rooms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouseType;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomHouseTypeRepository;
import com.xiaoshuidi.cloud.module.rooms.service.RoomHouseTypeService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 户型信息 服务实现类
 * </p>
 *
 * @author hans.zhang
 * @since 2023-05-09
 */
@Service
@RequiredArgsConstructor
public class RoomHouseTypeServiceImpl implements RoomHouseTypeService {

    private final RoomHouseTypeRepository roomHouseTypeRepository;
    private final RedisTemplate redisTemplate;

    /**
     * 户型分页
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public CommonResult<PageResult<RoomHouseType>> pageList(@NonNull Integer pageNum, @NonNull Integer pageSize, String name) {
        LambdaQueryWrapper<RoomHouseType> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotEmpty(name), RoomHouseType::getName, name);
        wrapper.orderByDesc(RoomHouseType::getCreateTime);
        Page page = roomHouseTypeRepository.page(new Page(pageNum, pageSize), wrapper);
        return CommonResult.success(new PageResult<RoomHouseType>(page.getRecords(), page.getTotal()));
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public RoomHouseType getRoomHouseType(Integer bedRoomNum, Integer livingRoomNum, Integer toiletNum, Integer kitchenNum, Integer balcony) {
        // 卧室
        bedRoomNum = bedRoomNum == null ? 0 : bedRoomNum;
        // 客厅
        livingRoomNum = livingRoomNum == null ? 0 : livingRoomNum;
        // 卫生间
        toiletNum = toiletNum == null ? 0 : toiletNum;
        // 厨房
        kitchenNum = kitchenNum == null ? 0 : kitchenNum;
        // 阳台
        balcony = balcony == null ? 0 : balcony;
        //先从redis 缓存获取
        String redisKey = String.format("rooms:house_type:%s_%s_%s_%s_%s", bedRoomNum, livingRoomNum, toiletNum, kitchenNum, balcony);
        Object houseTypeObj = redisTemplate.opsForValue().get(redisKey);
        RoomHouseType roomHouseType;
        if (ObjectUtil.isEmpty(houseTypeObj)) {
            roomHouseType = roomHouseTypeRepository.getLayout(bedRoomNum, livingRoomNum, toiletNum, kitchenNum, balcony);
        } else {
            roomHouseType = JSONUtil.toBean(houseTypeObj.toString(), RoomHouseType.class);
        }

        // 户型不存在则创建户型
        if (ObjectUtil.isEmpty(roomHouseType)) {
            roomHouseType = new RoomHouseType();
            roomHouseType.setBedRoomNum(bedRoomNum);
            roomHouseType.setLivingRoomNum(livingRoomNum);
            roomHouseType.setToiletNum(toiletNum);
            roomHouseType.setKitchenNum(kitchenNum);
            roomHouseType.setBalcony(balcony);
            StringBuilder name = new StringBuilder();
            if (roomHouseType.getBedRoomNum() != null && roomHouseType.getBedRoomNum() > 0) {
                name.append(roomHouseType.getBedRoomNum());
                name.append("室");
            }
            if (roomHouseType.getLivingRoomNum() != null && roomHouseType.getLivingRoomNum() > 0) {
                name.append(roomHouseType.getLivingRoomNum());
                name.append("厅");
            }
            if (roomHouseType.getKitchenNum() != null && roomHouseType.getKitchenNum() > 0) {
                name.append(roomHouseType.getKitchenNum());
                name.append("厨");
            }
            if (roomHouseType.getToiletNum() != null && roomHouseType.getToiletNum() > 0) {
                name.append(roomHouseType.getToiletNum());
                name.append("卫");
            }
            if (roomHouseType.getBalcony() != null && roomHouseType.getBalcony() > 0) {
                name.append(roomHouseType.getBalcony());
                name.append("阳台");
            }
            roomHouseType.setName(name.toString());
            roomHouseTypeRepository.save(roomHouseType);
        }
        redisTemplate.opsForValue().set(redisKey, JSONUtil.toJsonStr(roomHouseType));
        return roomHouseType;
    }
}
